import 'dart:convert';

import 'package:flutter/widgets.dart';
import 'package:netease_cloud_music_api/utils/crypto.dart';
import 'package:netease_cloud_music_api/utils/enum.dart';
import 'package:netease_cloud_music_api/utils/player_request.dart';
import 'package:netease_cloud_music_api/utils/request.dart';

mixin Album {
  /// 专辑内容
  /// [id] 传入专辑ID
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> album({required int id}) {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/v1/album/${id.toString()}',
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        ua: UserAgent.pc,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 已收藏专辑列表
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 ,如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumSublist({int? limit, int? offset}) {
    Map<String, dynamic> data = {'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true};
    return PlayerRequest(
      url: 'https://music.163.com/weapi/album/sublist',
      data: data,
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 收藏/取消收藏专辑
  /// [collect] 是否收藏 true 为收藏，false 为取消收藏
  /// [id] 专辑 id
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumSub({required bool collect, required int id}) {
    Map<String, dynamic> data = {'id': id};
    return PlayerRequest(
      url: 'https://music.163.com/api/album/${collect ? 'sub' : 'unsub'}',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 数字专辑&数字单曲-榜单
  /// [albumType] true 为数字专辑，false 为单曲
  /// [type] daily:日榜,week:周榜,year:年榜,total:总榜
  /// [year] 当 type 为 [SongSaleBoardType.year] 时 year 为返回当前年份的数字专辑或单曲 默认为当前年份
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 ,如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumSongSaleBoard({required bool albumType, SongSaleBoardType? type, int? year, int? limit, int? offset}) {
    SongSaleBoardType songSaleBoardType = type ?? SongSaleBoardType.daily;
    String _type = 'daily';
    Map<String, dynamic> data = Map<String, dynamic>();
    DateTime dateTime = DateTime.now();
    if (songSaleBoardType == SongSaleBoardType.daily) {
      _type = 'daily';
    } else if (songSaleBoardType == SongSaleBoardType.total) {
      _type = 'total';
    } else if (songSaleBoardType == SongSaleBoardType.week) {
      _type = 'week';
    } else if (songSaleBoardType == SongSaleBoardType.year) {
      _type = 'year';
      data['year'] = year ?? dateTime.year;
    }
    data['albumType'] = albumType ? '1' : '0';
    data['limit'] = limit ?? 30;
    data['offset'] = offset ?? 0;
    return PlayerRequest(
      url: 'https://music.163.com/api/feealbum/songsaleboard/$_type/type',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 最新专辑
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> newAlbumEst() {
    return PlayerRequest(
      url: 'https://music.163.com/api/discovery/newAlbum',
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 全部新碟
  /// [albumArea] ALL:全部,ZH:华语,EA:欧美,KR:韩国,JP:日本
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> newAlbum({AlbumArea? albumArea, int? limit, int? offset}) {
    Map<String, dynamic> map = Map<String, dynamic>();
    map['limit'] = limit ?? 30;
    map['offset'] = offset ?? 0;
    map['total'] = true;
    map['area'] = 'ALL';
    if (albumArea == AlbumArea.ALL) map['area'] = 'ALL';
    if (albumArea == AlbumArea.EA) map['area'] = 'EA';
    if (albumArea == AlbumArea.ZH) map['area'] = 'ZH';
    if (albumArea == AlbumArea.KR) map['area'] = 'KR';
    if (albumArea == AlbumArea.JP) map['area'] = 'JP';
    return PlayerRequest(
      url: 'https://music.163.com/weapi/album/new',
      data: map,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 数字专辑-新碟上架
  /// [albumArea] ALL:全部,ZH:华语,EA:欧美,KR:韩国,JP:日本
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumList({AlbumArea? albumArea, int? limit, int? offset}) {
    Map<String, dynamic> map = Map<String, dynamic>();
    map['limit'] = limit ?? 30;
    map['offset'] = offset ?? 0;
    map['total'] = true;
    map['area'] = 'ALL';
    if (albumArea == AlbumArea.ALL) map['area'] = 'ALL';
    if (albumArea == AlbumArea.EA) map['area'] = 'EA';
    if (albumArea == AlbumArea.ZH) map['area'] = 'ZH';
    if (albumArea == AlbumArea.KR) map['area'] = 'KR';
    if (albumArea == AlbumArea.JP) map['area'] = 'JP';
    return PlayerRequest(
      url: 'https://music.163.com/weapi/vipmall/albumproduct/list',
      data: map,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 数字专辑-语种风格馆
  /// [albumArea] ZH:华语,EA:欧美,KR:韩国,JP:日本
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumListStyle({AlbumArea? albumArea, int? limit, int? offset}) {
    Map<String, dynamic> map = Map<String, dynamic>();
    map['limit'] = limit ?? 30;
    map['offset'] = offset ?? 0;
    map['total'] = true;
    map['area'] = 'Z_H';
    if (albumArea == AlbumArea.EA) map['area'] = 'E_A';
    if (albumArea == AlbumArea.ZH) map['area'] = 'Z_H';
    if (albumArea == AlbumArea.KR) map['area'] = 'KR';
    if (albumArea == AlbumArea.JP) map['area'] = 'JP';
    return PlayerRequest(
      url: 'https://music.163.com/weapi/vipmall/appalbum/album/style',
      data: map,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 数字专辑详情
  /// [id] 专辑ID
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumDetail({required int id}) {
    Map<String, dynamic> data = {'id': id};
    return PlayerRequest(
      url: 'https://music.163.com/weapi/vipmall/albumproduct/detail',
      data: data,
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 专辑动态信息
  /// [id] 专辑ID
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> albumDetailDynamic({required int id}) {
    Map<String, dynamic> data = {'id': id};
    return PlayerRequest(
      url: 'https://music.163.com/api/album/detail/dynamic',
      data: data,
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 我的数字专辑
  /// 说明 : 登录后调用此接口 ,可获取我的数字专辑
  Future<PlayerRequestResponse> digitalAlbumPurchased({int? limit, int? offset}) {
    return PlayerRequest(
      url: 'https://music.163.com/api/digitalAlbum/purchased',
      data: {'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true},
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 购买数字专辑
  /// 说明 : 登录后调用此接口 ,可获取购买数字专辑的地址,把地址生成二维码后,可扫描购买专辑
  /// [id] 数字专辑id
  /// [AlbumPayment] 为支付方式, [AlbumPayment.Alipay] 为支付宝支付, [AlbumPayment.WeChat] 为微信支付
  /// [quantity] 购买数量, 默认为 1
  Future<PlayerRequestResponse> digitalAlbumOrdering({required int id, required AlbumPayment payment, int? quantity}) {
    Map<String, dynamic> data = {
      'business': 'Album',
      'digitalResources': jsonEncode({ 'business': 'Album', 'resourceID': id, 'quantity': quantity ?? 1 }),
      'from': 'web',
    };
    if (payment != null) {
      if (payment == AlbumPayment.WeChat) data['paymentMethod'] = 3;
      if (payment == AlbumPayment.Alipay) data['paymentMethod'] = 0;
    } else {
      data['paymentMethod'] = 3;
    }
    return PlayerRequest(
      url: 'https://music.163.com/api/ordering/web/digital',
      data: data,
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }
}